package com.qqtech.qquan.homemaking.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.enums.HomemakingTypeStatusDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingType;
import com.qqtech.qquan.homemaking.model.HomemakingTypeQuery;
import com.qqtech.qquan.homemaking.model.HomemakingTypeVo;
import com.qqtech.qquan.homemaking.service.HomemakingTypeService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/homemaking/type")
public class HomemakingTypeController extends BaseAdminController {
	@Resource
	private HomemakingTypeService homemakingTypeService;

	@Override
	protected BaseService<HomemakingType> getBaseService() {
		return homemakingTypeService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<HomemakingTypeVo> typeList = homemakingTypeService
				.findParent(null);
		model.addAttribute("typeList", typeList);
		return "/homemaking/homemakingtype-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(HomemakingTypeQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<HomemakingTypeVo> page = homemakingTypeService.queryPageListAdmin(
				query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Integer id, Model model) {
		HomemakingTypeVo homemakingTypeVo = homemakingTypeService.getById(id);
		if (homemakingTypeVo != null && homemakingTypeVo.getParentId() != null
				&& homemakingTypeVo.getParentId().intValue() != 0) {
			HomemakingTypeVo parentTypeVo = homemakingTypeService.getById(homemakingTypeVo.getParentId());
			homemakingTypeVo.setParentName(parentTypeVo.getName());
		}
		model.addAttribute("homemakingTypeVo", homemakingTypeVo);
		return "/homemaking/homemakingtype-edit";
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result doEdit(HomemakingTypeVo homemakingTypeVo) {
		Result ret = new Result();
		if (homemakingTypeVo == null || homemakingTypeVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(homemakingTypeVo.getName())) {
				ret.addErr("名称不能为空");
			}
			if (homemakingTypeVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
		}
		if (ret.checkForm()) {
			HomemakingType update = new HomemakingType();
			update.setId(homemakingTypeVo.getId());
			update.setName(homemakingTypeVo.getName().trim());
			update.setStatus(homemakingTypeVo.getStatus());
			update.setImg(homemakingTypeVo.getImg()==null?"":homemakingTypeVo.getImg());
			update.setOidx(homemakingTypeVo.getOidx()==null?0:homemakingTypeVo.getOidx());
			update.setRemark(homemakingTypeVo.getRemark()==null?"":homemakingTypeVo.getRemark());
			homemakingTypeService.updateById(update);
		}
		return ret;
	}
	
	/**
	 * 获取一级类别
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findParent")
	@ResponseBody
	public Result findParent() {
		Result result = new Result();
		List<HomemakingTypeVo> list = homemakingTypeService
				.findParent(null);
		result.put("parentList", list);
		return result;
	}

	/**
	 * 获取二级类别
	 * 
	 * @param parentId
	 *            一级id
	 * @return
	 */
	@RequestMapping(value = "/findChildren")
	@ResponseBody
	public Result findChildren(Integer parentId) {
		Result result = new Result();
		if (parentId != null) {
			List<HomemakingTypeVo> list = homemakingTypeService.findChildren(parentId, null);
			result.put("childrenList", list);
		}
		return result;
	}
}
